﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ChatIBOXClient
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
        private TaskbarNotify taskbarNotify;
        private ServiceReference.Client localClient = null;
        private ProxySingleton proxySingleton = ProxySingleton.GetInstance();

        // Detect a window if it's opened
        public static Dictionary<string, ChatWindow> listChatting = new Dictionary<string, ChatWindow>();


		public MainWindow()
		{
            taskbarNotify = new TaskbarNotify();
            this.InitializeComponent();
            taskbarNotify.Show();

			// Insert code required on object creation below this point.

            this.SignInControl.ButtonSignInClick += new RoutedEventHandler(SignInControl_SignInButtonClick);
            this.SignInControl.ButtonLoginClick += new RoutedEventHandler(SignInControl_SignInButtonClick);
            this.SignInControl.textboxName.Focus();
            proxySingleton.ProxyCallbackEvent += new ProxySingleton.ProxyCallbackEventHander(this.SignInControl.ProxySingleton_ProxyCallbackEvent);
            proxySingleton.ProxyCallbackEvent += new ProxySingleton.ProxyCallbackEventHander(this.ProxySingleton_ProxyCallbackEvent);
		}

        private void ProxySingleton_ProxyCallbackEvent(object sender, ProxyCallbackEventArgs e)
        {
            switch (e.callbackType)
            {
                case CallbackType.Receive:
                    ShowChatWindow(e.message);
                    break;
                case CallbackType.RefreshClient:
                    break;
                case CallbackType.UserJoin:
                    //this.UserJoin(e.client);
                    break;
                case CallbackType.HasNotify:
                    this.HasNotify(e.notify);
                    break;
                case CallbackType.UserLeave:
                    break;
                default:
                    break;
            }
        }

        private void UserJoin(ServiceReference.Client client)
        {
            //labelStatus.Content = client.Name + " is online now.";
        }

        private void ShowChatWindow(ServiceReference.Message message)
        {
            if (!listChatting.ContainsKey(message.Sender))
            {
                ChatWindow chatWindow = new ChatWindow();
                chatWindow.Visibility = Visibility.Visible;
                chatWindow.Title = "Chat with " + message.Sender;
                chatWindow.Receiver.Name = message.Sender;
                chatWindow.ReceiveMessage(message);

                // Add a window to dictionary
                listChatting.Add(message.Sender, chatWindow);
            }
            
        }
        
        private void SignInControl_SignInButtonClick(object sender, RoutedEventArgs e)
        {
            localClient = this.SignInControl.CurrentClient;
            proxySingleton.localClient = localClient;
            proxySingleton.Connect();
            this.SignInControl.Visibility = Visibility.Hidden;
            this.ListChatControl.Visibility = Visibility.Visible;
            this.menuDisconnect.IsEnabled = true;
        }

        private void menuDisconnect_Click(object sender, RoutedEventArgs e)
        {
            proxySingleton.Disconnect();
            this.SignInControl.Visibility = Visibility.Visible;
            this.ListChatControl.Visibility = Visibility.Hidden;
        }

        private void NotifyIcon_DoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                // Open the TaskbarNotifier
                this.taskbarNotify.Notify();
            }
        }

        private void HasNotify(string message)
        {
            this.taskbarNotify.NotifyContent.Insert(0, new NotifyObject(message));
            this.taskbarNotify.Notify();
        }
	}
}